// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 2008-7-11 22:40:08
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   AbsQueryMap.java

package com.creawor.imei.base;

import java.io.PrintStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import net.sf.hibernate.*;

// Referenced classes of package com.creawor.imei.base:
//            DBUtil, QueryBean

public class AbsQueryMap
{

    public AbsQueryMap()
    {
        totalrow = 1;
        pagesize = new Integer(10);
        pageno = new Integer(1);
        limitsql = "";
    }

    public Integer getPagesize()
    {
        return pagesize;
    }

    public void setPagesize(int i)
    {
        pagesize = new Integer(i);
    }

    public Integer getPageno()
    {
        return pageno;
    }

    public void setPageno(int i)
    {
        pageno = new Integer(i);
    }

    public void setQueryPage(Query query)
    {
        query.setFirstResult((pageno.intValue() - 1) * pagesize.intValue());
        query.setMaxResults(pagesize.intValue());
    }

    public void initSession()
        throws HibernateException
    {
        session = DBUtil.currentSession();
        session.clear();
    }

    public void closeSession()
        throws HibernateException
    {
        DBUtil.closeSession();
    }

    public Session getSession()
    {
        return session;
    }

    public int getTotalrow()
    {
        return totalrow;
    }

    public void setPageno(Integer integer)
    {
        pageno = integer;
    }

    public void setPagesize(Integer integer)
    {
        pagesize = integer;
    }

    public void setSession(Session session)
    {
        this.session = session;
    }

    public void setTotalrow(int i)
    {
        totalrow = i;
    }

    public Iterator findAll(String sql_string, HashMap sql_param)
        throws HibernateException, SQLException, ParseException
    {
        QueryBean qb = new QueryBean();
        String table = null;
        if(sql_string == null && sql_string.equalsIgnoreCase("null") && !sql_string.split(" ")[0].equalsIgnoreCase("from"))
        {
            //System.out.println("ERROR: sql\u8BED\u53E5\u4E0D\u6B63\u786E\uFF01");
            //System.out.println(sql_string);
        	
            return null;
        }
        table = sql_string.split(" ")[1];
        Iterator iterator = null;
        Query query_ct = session.createQuery((new StringBuilder("select count(*) ")).append(sql_string).toString());
        if(sql_param == null)
        {
            //System.out.println("ERROR: SQL\u53C2\u6570\u4E0D\u6B63\u786E\uFF01");
        	sql_param=new HashMap();
//            return null;
        }
        qb.conn = session.connection();
        qb.connect();
        qb.SQLstr = (new StringBuilder("select * from ")).append(table.toLowerCase()).append(" where 1=2").toString();
        qb.executeQuery();
        for(iterator = sql_param.keySet().iterator(); iterator.hasNext();)
        {
            String field = (String)iterator.next();
            String value = (String)sql_param.get(field);
            String dflag = field.substring(field.length() - 2);
            if(dflag.equals("_1") || dflag.equals("_2"))
                field = field.substring(0, field.length() - 2);
            SimpleDateFormat sdf = null;
            switch(qb.getFieldType(field))
            {
            case -7: 
            case -6: 
                query_ct.setBoolean(field, Boolean.getBoolean(value));
                break;

            case 93: // ']'
                sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                query_ct.setTimestamp(field, sdf.parse(value));
                break;

            case 91: // '['
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                query_ct.setTimestamp(field, sdf.parse(value));
                break;

            case 92: // '\\'
                sdf = new SimpleDateFormat("hh:mm:ss");
                query_ct.setTimestamp(field, sdf.parse(value));
                break;

            case 2: // '\002'
            case 3: // '\003'
                query_ct.setBigDecimal(field, new BigDecimal(value));
                break;

            case 8: // '\b'
                query_ct.setDouble(field, Double.parseDouble(value));
                break;

            case 6: // '\006'
            case 7: // '\007'
                query_ct.setFloat(field, Float.parseFloat(value));
                break;

            case -5: 
                query_ct.setLong(field, Long.parseLong(value));
                break;

            case 5: // '\005'
                query_ct.setShort(field, Short.parseShort(value));
                break;

            case -1: 
            case 1: // '\001'
            case 12: // '\f'
                query_ct.setString(field, value);
                break;

            default:
                query_ct.setString(field, value);
                break;
            }
        }

        totalrow = ((Integer)query_ct.iterate().next()).intValue();
        Query query = session.createQuery(sql_string);
        if(sql_param != null)
            for(iterator = sql_param.keySet().iterator(); iterator.hasNext();)
            {
                String field = (String)iterator.next();
                String value = (String)sql_param.get(field);
                String dflag = field.substring(field.length() - 2);
                if(dflag.equals("_1") || dflag.equals("_2"))
                    field = field.substring(0, field.length() - 2);
                SimpleDateFormat sdf = null;
                switch(qb.getFieldType(field))
                {
                case -7: 
                case -6: 
                    query.setBoolean(field, Boolean.getBoolean(value));
                    break;

                case 93: // ']'
                    sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    query.setTimestamp(field, sdf.parse(value));
                    break;

                case 91: // '['
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    query.setTimestamp(field, sdf.parse(value));
                    break;

                case 92: // '\\'
                    sdf = new SimpleDateFormat("hh:mm:ss");
                    query.setTimestamp(field, sdf.parse(value));
                    break;

                case 2: // '\002'
                case 3: // '\003'
                    query.setBigDecimal(field, new BigDecimal(value));
                    break;

                case 8: // '\b'
                    query.setDouble(field, Double.parseDouble(value));
                    break;

                case 6: // '\006'
                case 7: // '\007'
                    query.setFloat(field, Float.parseFloat(value));
                    break;

                case -5: 
                    query.setLong(field, Long.parseLong(value));
                    break;

                case 5: // '\005'
                    query.setShort(field, Short.parseShort(value));
                    break;

                case -1: 
                case 1: // '\001'
                case 12: // '\f'
                    query.setString(field, value);
                    break;

                default:
                    query.setString(field, value);
                    break;
                }
            }

        setQueryPage(query);
        return query.iterate();
    }

    public List findAllList(String sql_string, HashMap sql_param)
    throws HibernateException, SQLException, ParseException
{
    QueryBean qb = new QueryBean();
    String table = null;
    if(sql_string == null && sql_string.equalsIgnoreCase("null") && !sql_string.split(" ")[0].equalsIgnoreCase("from"))
    {
        //System.out.println("ERROR: sql\u8BED\u53E5\u4E0D\u6B63\u786E\uFF01");
        //System.out.println(sql_string);
        return null;
    }
    table = sql_string.split(" ")[1];
    Iterator iterator = null;
    Query query_ct = session.createQuery((new StringBuilder("select count(*) ")).append(sql_string).toString());
    if(sql_param == null)
    {
        //System.out.println("ERROR: SQL\u53C2\u6570\u4E0D\u6B63\u786E\uFF01");
    	sql_param=new HashMap();
       // return null;
        
    }
    qb.conn = session.connection();
    qb.connect();
    qb.SQLstr = (new StringBuilder("select * from ")).append(table.toLowerCase()).append(" where 1=2").toString();
    qb.executeQuery();
    for(iterator = sql_param.keySet().iterator(); iterator.hasNext();)
    {
        String field = (String)iterator.next();
        String value = (String)sql_param.get(field);
        String dflag = field.substring(field.length() - 2);
        if(dflag.equals("_1") || dflag.equals("_2"))
            field = field.substring(0, field.length() - 2);
        SimpleDateFormat sdf = null;
        switch(qb.getFieldType(field))
        {
        case -7: 
        case -6: 
            query_ct.setBoolean(field, Boolean.getBoolean(value));
            break;

        case 93: // ']'
            sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            query_ct.setTimestamp(field, sdf.parse(value));
            break;

        case 91: // '['
            sdf = new SimpleDateFormat("yyyy-MM-dd");
            query_ct.setTimestamp(field, sdf.parse(value));
            break;

        case 92: // '\\'
            sdf = new SimpleDateFormat("hh:mm:ss");
            query_ct.setTimestamp(field, sdf.parse(value));
            break;

        case 2: // '\002'
        case 3: // '\003'
            query_ct.setBigDecimal(field, new BigDecimal(value));
            break;

        case 8: // '\b'
            query_ct.setDouble(field, Double.parseDouble(value));
            break;

        case 6: // '\006'
        case 7: // '\007'
            query_ct.setFloat(field, Float.parseFloat(value));
            break;

        case -5: 
            query_ct.setLong(field, Long.parseLong(value));
            break;

        case 5: // '\005'
            query_ct.setShort(field, Short.parseShort(value));
            break;

        case -1: 
        case 1: // '\001'
        case 12: // '\f'
            query_ct.setString(field, value);
            break;

        default:
            query_ct.setString(field, value);
            break;
        }
    }

    totalrow = ((Integer)query_ct.iterate().next()).intValue();
    Query query = session.createQuery(sql_string);
    if(sql_param != null)
        for(iterator = sql_param.keySet().iterator(); iterator.hasNext();)
        {
            String field = (String)iterator.next();
            String value = (String)sql_param.get(field);
            String dflag = field.substring(field.length() - 2);
            if(dflag.equals("_1") || dflag.equals("_2"))
                field = field.substring(0, field.length() - 2);
            SimpleDateFormat sdf = null;
            switch(qb.getFieldType(field))
            {
            case -7: 
            case -6: 
                query.setBoolean(field, Boolean.getBoolean(value));
                break;

            case 93: // ']'
                sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                query.setTimestamp(field, sdf.parse(value));
                break;

            case 91: // '['
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                query.setTimestamp(field, sdf.parse(value));
                break;

            case 92: // '\\'
                sdf = new SimpleDateFormat("hh:mm:ss");
                query.setTimestamp(field, sdf.parse(value));
                break;

            case 2: // '\002'
            case 3: // '\003'
                query.setBigDecimal(field, new BigDecimal(value));
                break;

            case 8: // '\b'
                query.setDouble(field, Double.parseDouble(value));
                break;

            case 6: // '\006'
            case 7: // '\007'
                query.setFloat(field, Float.parseFloat(value));
                break;

            case -5: 
                query.setLong(field, Long.parseLong(value));
                break;

            case 5: // '\005'
                query.setShort(field, Short.parseShort(value));
                break;

            case -1: 
            case 1: // '\001'
            case 12: // '\f'
                query.setString(field, value);
                break;

            default:
                query.setString(field, value);
                break;
            }
        }

    setQueryPage(query);
    return query.list();
}
    public Object get(Class clazz, Serializable id)
    {
        try
        {
            Transaction tran = session.beginTransaction();
            Object result = session.get(clazz, id);
            tran.commit();
            return result;
        }
        catch(HibernateException e)
        {
            throw new RuntimeException(e);
        }
    }

    public void remove(Class clazz, Serializable id)
    {
        try
        {
            Transaction tran = session.beginTransaction();
            session.delete(get(clazz, id));
            tran.commit();
        }
        catch(HibernateException e)
        {
            throw new RuntimeException(e);
        }
    }

    public void save(Object obj)
    {
        try
        {
            Transaction tran = session.beginTransaction();
            session.saveOrUpdate(obj);
            tran.commit();
        }
        catch(HibernateException e)
        {
            throw new RuntimeException(e);
        }
    }

    public Session session;
    public int totalrow;
    public Integer pagesize;
    public Integer pageno;
    public String limitsql;
}